'use strict';
app.controller('accountShopCtrl',['$scope','$stateParams','utilService','accountService','toaster', function($scope,$stateParams,utilService,accountService,toaster){
    $scope.systemUser = {};
    $scope.systemUser.platform=0;
    $scope.systemUser.isMainAccount=1;

    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'system/user',
            params:$scope.systemUser
        }).then(function(result){
        });
    }
    $scope.query();


    $scope.accountService=accountService;

    $scope.updateStatus = function(systemId,status){
        $scope.systemId=systemId;
        $scope.status=status==1?0:1;

        accountService.updateStatus($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query();
                    break;
                case 624:
                    toaster.pop('waiting', "提示", result.message);
                    $state.go('app.404');
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }


}]);

app.controller('accountOperationCtrl',['$scope','$stateParams','utilService', 'toaster', '$modal',
    function($scope,$stateParams,utilService, toaster, $modal){
        $scope.systemUser = {};
        $scope.systemUser.platform=1;

        // call server.
        $scope.query = function(){
            utilService.queryForPagingList($scope,{
                url:$scope.app.domain + 'system/user',
                params:$scope.systemUser
            }).then(function(result){
            });
        }
        $scope.query();

        // 激活帐号
        $scope.activation = function(systemId, name){
            $scope.modal = {
                title: '帐号激活',
                message: '确定激活"'+ name +'"代理商帐号？'
            }

            utilService.modal($modal, $scope, {
                title: $scope.modal.title,
                controller: "activationOperationAccountCtrl",
                message: $scope.modal.message,
                data:{
                    systemId: systemId,
                    status: 0 // 激活
                }
            });
        }

        // 取消激活
        $scope.cancelActivation = function(systemId, name){
            $scope.modal = {
                title: '取消激活',
                message: '确定取消激活"'+ name +'"代理商帐号？'
            }

            utilService.modal($modal, $scope, {
                title: $scope.modal.title,
                controller: "activationOperationAccountCtrl",
                message: $scope.modal.message,
                data:{
                    systemId: systemId,
                    status: 1 // 取消激活
                }
            });
        }
}]);

app.controller('accountInternalCtrl',['$scope','$stateParams','utilService','accountService','toaster', function($scope,$stateParams,utilService,accountService,toaster){
    $scope.systemUser = {};
    $scope.systemUser.platform=2;

    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'system/user',
            params:$scope.systemUser
        }).then(function(result){
        });
    }
    $scope.query();

    $scope.updateSystemUserStatus = function(id,status){
        $scope.systemUserId=id;
        $scope.status=status==1?0:1;

        accountService.updateSystemUserStatus($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query();
                    break;
                case 624:
                    toaster.pop('waiting', "提示", result.message);
                    $state.go('app.404');
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

}]);

app.controller('createAccountCtrl',['$scope','$state','$stateParams','accountService','toaster', function($scope,$state,$stateParams,accountService,toaster){
    $scope.systemUser = {
        isUpdatePassword:1,
        gender:1,
        platform:2
    };

    //查询所有角色信息
    $scope.getAll=function(){
        $scope.roleListVO={};
        $scope.roleListVO.platform=2;

        accountService.getAll($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.roles=result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.getAll();

    //选中、
    $scope.checkItem=function(item){
        item.isSelect==0?item.isSelect=1:item.isSelect=0;
    }

    $scope.createAccount = function(){
        accountService.createSystemUser($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.accountInternal');
                    break;
                case 602:
                case 603:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

}]);

app.controller('editShopAccountCtrl',['$scope','$state','$stateParams','accountService','toaster', function($scope,$state,$stateParams,accountService,toaster){

    $scope.systemUser = {
        isUpdatePassword:1,
        gender:1,
        systemUserId:$stateParams.systemUserId
    };

    // query data.
    $scope.findById=function(){
        accountService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.systemUser = result.data;
                    break;
                case 601:
                    toaster.pop('waiting', "提示", result.message);
                    $state.go('app.404');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }

        });
    }

    $scope.findById();

    // update event.
    $scope.updateAccount = function(){
        accountService.updateSystemUser($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.accountShop');
                    break;
                case 602:
                case 603:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    //查询所有角色信息
    $scope.getAll=function(){
        $scope.roleListVO={};
        $scope.roleListVO.systemId=$stateParams.systemId;
        $scope.roleListVO.platform=0;
        $scope.roleListVO.systemUserId=$stateParams.systemUserId;

        accountService.getAll($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.roles=result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.getAll();

    //选中、
    $scope.checkItem=function(item){
        item.isSelect==0?item.isSelect=1:item.isSelect=0;
    }

}]);

app.controller('activationOperationAccountCtrl',['$scope','$state','$stateParams','accountService','toaster', 'modalData', '$modalInstance',
        function($scope,$state,$stateParams,accountService,toaster, modalData, $modalInstance){

            var systemId = modalData.data.systemId;
            var status = modalData.data.status;

            $scope.isAllowSubmit=true;
            $scope.ok = function() {
                if(!$scope.isAllowSubmit){
                    return;
                }
                $scope.isAllowSubmit = false;

                accountService.switchActivationStatus($scope, systemId, status).then(function(result){
                    switch (result.status){
                        case 200:
                            $scope.query();
                            toaster.pop('success', "提示", "操作成功");
                            $scope.cancel();
                            break;
                        default:
                            toaster.pop('error', "操作失败", result.message);
                            break
                    }
                });
                $scope.isAllowSubmit=true;
            }

            //取消弹窗
            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
}]);

app.controller('editInternalAccountCtrl',['$scope','$state','$stateParams','accountService','toaster', function($scope,$state,$stateParams,accountService,toaster){

    $scope.systemUser = {
        isUpdatePassword:1,
        gender:1,
        systemUserId:$stateParams.systemUserId
    };

    // query data.
    $scope.findById=function(){
        accountService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.systemUser = result.data;
                    break;
                case 601:
                    toaster.pop('waiting', "提示", result.message);
                    $state.go('app.404');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }

        });
    }

    $scope.findById();

    // update event.
    $scope.updateAccount = function(){
        accountService.updateSystemUser($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.accountInternal');
                    break;
                case 602:
                case 603:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    //查询所有角色信息
    $scope.getAll=function(){
        $scope.roleListVO={};
        $scope.roleListVO.systemId=$stateParams.systemId;
        $scope.roleListVO.platform=2;
        $scope.roleListVO.systemUserId=$stateParams.systemUserId;

        accountService.getAll($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.roles=result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.getAll();

    //选中、
    $scope.checkItem=function(item){
        item.isSelect==0?item.isSelect=1:item.isSelect=0;
    }

}]);


app.controller('editInternalNowAccountCtrl',['$scope','$state','$stateParams','accountService','toaster','$cookies',
    function($scope,$state,$stateParams,accountService,toaster,$cookies){
    //$scope.isUpdateMobilePhone=0;
    $scope.systemUser = {
        isUpdatePassword:0,
        gender:1
    }

    // query data.
    $scope.findBySessionId=function(){
        accountService.findBySessionId($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.systemUser = result.data;
                    $scope.systemUser.isUpdatePassword = 0;
                    //$scope.systemUser.oldMobilePhone=result.data.mobilePhone;
                    //$scope.systemUser.phone = result.data.mobilePhone.substring(0,3)+'****'+result.data.mobilePhone.substring(7,11);
                    //$scope.systemUser.isUpdateMobilePhone=$scope.isUpdateMobilePhone;
                    break;
                case 601:
                    toaster.pop('waiting', "提示", result.message);
                    $state.go('app.404');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }

        });
    }

    $scope.findBySessionId();


    //修改
    $scope.updateNowAccount = function () {
        var systemUserToken = "mobilePhone="+$scope.systemUser.mobilePhone+"&oldPassword="+$scope.systemUser.oldPassword+
            "&password="+$scope.systemUser.password+"&name="+$scope.systemUser.name+"&email="+$scope.systemUser.email;
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(publicKey);
        $scope.systemUser.systemUserToken = encodeURIComponent(encrypt.encrypt(systemUserToken));
        $scope.systemUser.isUpdateMobilePhone = 0;
        $scope.isUpdatePwd=$scope.systemUser.isUpdatePassword;

        accountService.updateNowSystemUser($scope).then(function(result){
            switch (result.status){
                case 200:
                    //if($scope.isUpdatePwd == 1 || $scope.isUpdateMobilePhone == 1){
                    if($scope.isUpdatePwd == 1){
                        //$modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功,请重新登录");
                        delete $cookies.sessionId;
                        $scope.app.session = {};
                        location.href = '#/access/login';
                    }else{
                        //$modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $state.go('app.nowAccountInternalEdit');
                    }
                    break;
                case 603:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });

    };
}]);
